package com.qh.resourcehub.info.virtualresource.dao.impl;

import com.qh.resourcehub.info.entity.ApplicationData;
import com.qh.resourcehub.info.virtualresource.dao.VmSupervisorReviewDao;
import com.qh.resourcehub.utils.XuguUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.*;
import java.util.*;

@Repository("VmSupervisorReviewDao")
public class VmSupervisorReviewDaoImpl implements VmSupervisorReviewDao {
    @Autowired
    private XuguUtils xuguUtils;

    @Override
    public List<Map<String, Object>> queryVmReviewData(ApplicationData applicationData) {
        List<Map<String, Object>> result = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pstmtUser = null;

        try {
            connection = xuguUtils.getConnection();
            List<Object> queryParams = new ArrayList<>();

            // 基本查询语句
            StringBuilder sqlQuery = new StringBuilder("SELECT * FROM APPLICATION_DATA WHERE 1=1 ");

            // 根据当前用户ID匹配 REVIEWER 字段
            if (!StringUtils.isEmpty(applicationData.getApplyUser())) {
                sqlQuery.append(" AND REVIEWER = ?");
                queryParams.add(applicationData.getApplyUser());
            }
            // PROCESS_STATUS 固定为 01
            sqlQuery.append(" AND PROCESS_STATUS = '01'");
            // 分页参数
            int page = String.valueOf(applicationData.getPage()) != null ? applicationData.getPage() : 0;
            int size = String.valueOf(applicationData.getSize())!= null ? applicationData.getSize() : 10;
            int offset = page * size;

            // 添加 LIMIT 和 OFFSET
            sqlQuery.append(" LIMIT ? OFFSET ?");
            queryParams.add(size);
            queryParams.add(offset);

            // 创建 PreparedStatement
            pstmtUser = connection.prepareStatement(sqlQuery.toString());

            // 设置查询参数
            for (int i = 0; i < queryParams.size(); i++) {
                pstmtUser.setObject(i + 1, queryParams.get(i));
            }

            // 执行查询
            ResultSet rs = pstmtUser.executeQuery();
            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();
                // 获取ResultSet的元数据（列信息）
                ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                // 遍历所有列并将列名和值添加到Map中
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);  // 获取列名
                    Object columnValue = rs.getObject(i);  // 获取列值
                    row.put(columnName, columnValue);
                }
                // 将该行数据添加到结果列表中
                result.add(row);
            }
            return result; // 返回查询结果

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            try {
                if (pstmtUser != null) {
                    pstmtUser.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return Collections.emptyList(); // 如果发生异常，返回空列表
    }

    @Override
    public int VmReviewPass(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 修改操作
            StringBuilder sqlUser = new StringBuilder("UPDATE APPLICATION_DATA SET ");
            List<Object> values = new ArrayList<>();
            if (applicationData.getDataType() != null) {
                sqlUser.append("DATA_TYPE = ?, ");
                values.add(applicationData.getDataType());
            }
            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }
            sqlUser.setLength(sqlUser.length() - 2); // 去掉最后的逗号和空格
            sqlUser.append(" WHERE ID = ?");  // 设置 WHERE 条件
            values.add(applicationData.getId());  // 添加 ID 条件
            pstmtUser = connection.prepareStatement(sqlUser.toString());
            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                pstmtUser.setObject(i + 1, values.get(i));
            }
            result = pstmtUser.executeUpdate();  // 执行更新操作
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    public int VmReviewOverrule(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 修改操作
            StringBuilder sqlUser = new StringBuilder("UPDATE APPLICATION_DATA SET ");
            List<Object> values = new ArrayList<>();
            if (applicationData.getDataType() != null) {
                sqlUser.append("DATA_TYPE = ?, ");
                values.add(applicationData.getDataType());
            }
            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }
            sqlUser.setLength(sqlUser.length() - 2); // 去掉最后的逗号和空格
            sqlUser.append(" WHERE ID = ?");  // 设置 WHERE 条件
            values.add(applicationData.getId());  // 添加 ID 条件
            pstmtUser = connection.prepareStatement(sqlUser.toString());
            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                pstmtUser.setObject(i + 1, values.get(i));
            }
            result = pstmtUser.executeUpdate();  // 执行更新操作
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }
}
